package serve;

import po.Good;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/CartList")
public class CartList extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8"); // 设置生成的文档类型
        PrintWriter out = response.getWriter(); // 得到输出字符输出流
        out.println("<h1>你的购物车：</h1>");

        HttpSession session = req.getSession(false);
        if (session == null) {
            out.println("<h2>无商品</h2>");
            return;
        }

        List<Good> cart = (List<Good>) session.getAttribute("cart");
        if (cart == null || cart.isEmpty()) {
            out.println("<h2>无商品</h2>");
        } else {
            out.println("<ul>");
            for (Good good : cart) {
                out.println("<li>" + good.getName() + "</li>");
            }
            out.println("</ul>");
        }

        out.println("<a href='goods'>商品列表</a>");
    }
}
